package com.decent.manager.system.controller;

import com.decent.common.bo.SimpleMessage;
import com.decent.common.enums.ErrorCodeEnum;
import com.decent.common.validator.operate.Add;
import com.decent.common.validator.operate.Update;
import com.decent.manager.system.dto.SystemRoleDTO;
import com.decent.manager.system.service.SystemRoleService;
import com.decent.manager.system.vo.SystemRoleVO;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色管理
 *
 * @author wangyan
 **/
@RequestMapping("systemRole")
@RestController
public class SystemRoleController {
    @Resource
    private SystemRoleService systemRoleService;

    /**
     * 查询角色列表
     *
     * @return list
     */
    @RequestMapping("getRoleList")
    public List<SystemRoleVO> getRoleList() {
        return systemRoleService.getRoleList();
    }

    /**
     * 新增角色
     *
     * @param roleName 角色名称
     * @return SimpleMessage
     */
    @SuppressWarnings("JavadocReference")
    @RequestMapping("createRole")
    public SimpleMessage createRole(@Validated(Add.class) SystemRoleDTO role, BindingResult result) {
        if (result.hasErrors()) {
            return new SimpleMessage(ErrorCodeEnum.INVALID_PARAMS, result.getAllErrors().get(0).getDefaultMessage());
        }
        return systemRoleService.createRole(role);
    }

    /**
     * 修改角色
     *
     * @param id         角色id
     * @param roleName   角色名称
     * @param roleSort   排序
     * @param roleStatus 状态
     * @param remark     备注
     * @return SimpleMessage
     */
    @SuppressWarnings("JavadocReference")
    @RequestMapping("updateRole")
    public SimpleMessage updateRole(@Validated(Update.class) SystemRoleDTO role, BindingResult result) {
        if (result.hasErrors()) {
            return new SimpleMessage(ErrorCodeEnum.INVALID_PARAMS, result.getAllErrors().get(0).getDefaultMessage());
        }
        if (role.emptyUpdateParam()) {
            return new SimpleMessage(ErrorCodeEnum.INVALID_PARAMS, "请选择要修改的操作");
        }
        return systemRoleService.updateRole(role);
    }

    /**
     * 菜单授权
     *
     * @param id              角色id
     * @param grantMenuIdList 菜单id
     * @return SimpleMessage
     */
    @SuppressWarnings("JavadocReference")
    @RequestMapping("grantMenu")
    public SimpleMessage grantMenu(@Validated(SystemRoleDTO.GrantMenu.class) SystemRoleDTO role,
                                   BindingResult result) {
        if (result.hasErrors()) {
            return new SimpleMessage(ErrorCodeEnum.INVALID_PARAMS, result.getAllErrors().get(0).getDefaultMessage());
        }
        return systemRoleService.grantMenu(role);
    }
}
